package com.hhh.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hhh.Pojo.Employee;
import com.hhh.Pojo.Result;
import com.hhh.Service.EmployeeService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    //    添加员工
//    添加员工
//    添加员工
    @PostMapping
    public Result Insert(@RequestBody Employee employee, HttpServletRequest request) {
        long id = (long) request.getSession().getAttribute("employee");
        if (employeeService.Insert(employee, id) != 1)
            return Result.error("添加失败");
        else {
            log.info("插入员工数据");
            return Result.success("添加成功");
        }

    }

    //    查询员工
//    查询员工
//    查询员工
    @GetMapping("/page")
    public Result SelectByPage(@RequestParam long page, @RequestParam long pageSize, String name) {
        IPage<Employee> arr = employeeService.SelectList(page, pageSize, name);
        log.info("查询员工数据");
        return Result.success(arr);
    }

    //    修改员工数据
//    修改员工数据
//    修改员工数据
    @PutMapping()
    public Result Update(@RequestBody Employee employee,
                         HttpServletRequest request) {
        log.info("修改员工数据");
        long UpdateUserId = (long) request.getSession().getAttribute("employee");
        Integer returnresult = employeeService.Update(employee, UpdateUserId);
        if (returnresult != 1)
            return Result.error("修改失败");
        else
            return Result.success("1");
    }

    //    编辑员工数据
//    其实只要查询员工数据返回给前端
//    后面前端会自动跳转到@PutMapping()
//    public Result Update(@RequestBody Employee employee,
//                               HttpServletRequest request)
    @GetMapping("/{id}")
    public Result UpdateEmployee(@PathVariable String id) {
        log.info("编辑员工数据");
        return Result.success(employeeService.SelectById(Long.parseLong(id)));
    }
}
